package com.peanut.baby.mvp.calendar;

import com.tencent.mars.xlog.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by anonymouslystudio on 2017/9/5.
 */

public class CalUtil {
    private static final String TAG = CalUtil.class.getSimpleName();

    private static final int PAILUAN_OFFSET_DAY         = 14;
    private static final int SAFE_PERIOD_DAY_BEFORE     = 5;
    private static final int SAFE_PERIOD_DAY_AFTER      = 4;
    private static final int PAILUAN_ENDABLE_OFFSET     = 10;

    private static final int YUEJING_PERIOD_FEFAULT     = 5;


    /**
     * 今天是否还在月经期，默认月经期为七天
     * @param yuejingStartDateString
     * @return
     */
    public static boolean isInYuejingPeriod(String yuejingStartDateString){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        try {
            startDate = fmt.parse(yuejingStartDateString);
        } catch (ParseException e) {
            return false;
        }

        long timeStart = startDate.getTime();
        long timeToday = System.currentTimeMillis();

        long daysOffset=(timeToday-timeStart)/(1000*3600*24);

        System.out.println("isInYuejingPeriod: yuejingStartDateString "+yuejingStartDateString+" daysOffset "+daysOffset);
        return daysOffset < YUEJING_PERIOD_FEFAULT;
    }

    /**
     * 今天是否还在月经期，默认月经期为七天
     * @param yuejingStartDateString
     * @return
     */
    public static boolean isInYuejingPeriod(String yuejingStartDateString,String currentDateString){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date currentDate = null;
        try {
            startDate = fmt.parse(yuejingStartDateString);
            currentDate = fmt.parse(currentDateString);
        } catch (ParseException e) {
            return false;
        }

        long timeStart = startDate.getTime();
        long timeToday = currentDate.getTime();

        long daysOffset=(timeToday-timeStart)/(1000*3600*24);

        System.out.println("isInYuejingPeriod: yuejingStartDateString "+yuejingStartDateString+" daysOffset "+daysOffset);
        return daysOffset < YUEJING_PERIOD_FEFAULT;
    }

    public static Date getPailuanDay(Date yuejingStartDate){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String fmted = fmt.format(yuejingStartDate);
        return getPailuanDay(fmted);
    }

    public static String dateToString(Date date){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String fmted = fmt.format(date);
        return fmted;
    }
    public static Date stringToDate(String dateString){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date fmted = fmt.parse(dateString);
            return fmted;
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     * 依据月经开始时间计算排卵日
     * @param yuejingStartDateString
     * @return
     */
    public static Date getPailuanDay(String yuejingStartDateString){
        Date resultDate = null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        try {
            startDate = fmt.parse(yuejingStartDateString);
        } catch (ParseException e) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.DATE,PAILUAN_OFFSET_DAY);

        resultDate = new Date(calendar.getTimeInMillis());
        System.out.println("getPailuanDay: input "+yuejingStartDateString+" output "+fmt.format(resultDate));
        return resultDate;
    }

    /**
     * 计算当前日期是否还可以选择月经结束（当前日期距月经开始大于10天）
     * @param yuejingStartDateString
     * @return
     */
    public static boolean isYuejingEnable(String yuejingStartDateString){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        try {
            startDate = fmt.parse(yuejingStartDateString);
        } catch (ParseException e) {
            return false;
        }

        long timeStart = startDate.getTime();
        long timeToday = System.currentTimeMillis();

        long daysOffset=(timeToday-timeStart)/(1000*3600*24);

        System.out.println("isYuejingEnable: yuejingStartDateString "+yuejingStartDateString+" daysOffset "+daysOffset);
        return daysOffset < PAILUAN_ENDABLE_OFFSET;
    }

    public static int daysBetween(Date min,Date max){
        return daysBetween(CalUtil.dateToString(min),CalUtil.dateToString(max));
    }

    public static int daysBetweenPlusOne(Date min,Date max){
        return daysBetween(CalUtil.dateToString(min),CalUtil.dateToString(max))+1;
    }

    public static int daysBetween(String min,String max){
        Log.d(TAG,"daysBetween: "+min+" "+max);
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date minDate = fmt.parse(min);
            Date maxDate = fmt.parse(max);

            long timeStart = minDate.getTime();
            long timeToday = maxDate.getTime();

            int diff = Integer.parseInt(String.valueOf((timeToday-timeStart)/(1000*3600*24)));

            if(diff == 0){
                return 0;
            }else{
                return diff;
            }
        } catch (ParseException e) {
            return 0;
        }
    }

    public static Date[] getPregnantPeriodWithYuejingStartDate(Date yuejingStartDate){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String fmted = fmt.format(yuejingStartDate);
        return getPregnantPeriodWithYuejingStartDate(fmted);
    }

    public static Date[] getYuejingPeriodWithYuejingStartDate(Date yuejingStartDate){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String fmted = fmt.format(yuejingStartDate);
        return getYuejingPeriodWithYuejingStartDate(fmted);
    }

    public static Date[] getYuejingPeriodWithYuejingStartDate(String yuejingStartDate){
        Date resultDate[] = null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");


        Date startDate = null;
        try {
            startDate = fmt.parse(yuejingStartDate);
        } catch (ParseException e) {
            return null;
        }
        if(startDate == null){
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.DATE,YUEJING_PERIOD_FEFAULT);
        Date afterDate = new Date(calendar.getTimeInMillis());

        resultDate = new Date[2];

        resultDate[0] = startDate;
        resultDate[1] = afterDate;

        System.out.println("月经期: 月经开始时间 "+yuejingStartDate+"  "+yuejingStartDate+"~"+fmt.format(afterDate));
        return resultDate;
    }


    /**
     * 根据月经开始日期计算易孕期
     * @param yuejingStartDate
     * @return
     */
    public static Date[] getPregnantPeriodWithYuejingStartDate(String yuejingStartDate){
        Date resultDate[] = null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");


        Date pailuanDate = getPailuanDay(yuejingStartDate);
        if(pailuanDate == null){
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(pailuanDate);
        calendar.add(Calendar.DATE,-SAFE_PERIOD_DAY_BEFORE);

        Date beforeDate = new Date(calendar.getTimeInMillis());

        calendar.setTime(pailuanDate);
        calendar.add(Calendar.DATE,SAFE_PERIOD_DAY_AFTER);

        Date afterDate = new Date(calendar.getTimeInMillis());

        resultDate = new Date[2];

        resultDate[0] = beforeDate;
        resultDate[1] = afterDate;

        System.out.println("易孕期: 月经开始时间 "+yuejingStartDate+"  "+fmt.format(beforeDate)+"~"+fmt.format(afterDate));
        return resultDate;
    }

    /**
     * 依据排卵日计算易孕期
     * @param pailuanDay  排卵日
     * @return
     */
    public static Date[] getPregnantPeriod(String pailuanDay){
        Date resultDate[] = null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date pailuanDate = null;
        try {
            pailuanDate = fmt.parse(pailuanDay);
        } catch (ParseException e) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(pailuanDate);
        calendar.add(Calendar.DATE,-SAFE_PERIOD_DAY_BEFORE);

        Date beforeDate = new Date(calendar.getTimeInMillis());

        calendar.setTime(pailuanDate);
        calendar.add(Calendar.DATE,SAFE_PERIOD_DAY_AFTER);

        Date afterDate = new Date(calendar.getTimeInMillis());

        resultDate = new Date[2];

        resultDate[0] = beforeDate;
        resultDate[1] = afterDate;

        System.out.println("getSafePeriod: pailuanDay "+pailuanDay+" period "+fmt.format(beforeDate)+"~"+fmt.format(afterDate));
        return resultDate;
    }


}
